package org.jingouzhui.test.domain;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.jingouzhui.domain.strategy.model.entity.RaffleAwardEntity;
import org.jingouzhui.domain.strategy.model.entity.RaffleFactorEntity;
import org.jingouzhui.domain.strategy.service.IRaffleStrategy;
import org.jingouzhui.domain.strategy.service.armory.IStrategyArmory;
import org.jingouzhui.domain.strategy.service.rule.chain.impl.RuleWeightLogicChain;
import org.jingouzhui.domain.strategy.service.rule.filter.impl.RuleLockLogicFilter;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.util.ReflectionTestUtils;

import javax.annotation.Resource;
import java.util.concurrent.CountDownLatch;

/**
 * @description: 抽奖前置规则过滤测试
 * @author: jingouzhui
 * @date: 2025/4/27 18:01
 */
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class RaffleStrategyTest {
    @Resource
    private IRaffleStrategy raffleStrategy;




    @Resource
    private RuleWeightLogicChain ruleWeightLogicChain;

    @Resource
    private IStrategyArmory strategyArmory;

    @Resource
    private RuleLockLogicFilter ruleLockLogicFilter;


    @Before
    public void setUp() {
     /*   strategyArmory.assembleLotteryStrategy(100001L);
        strategyArmory.assembleLotteryStrategy(100002L);
        strategyArmory.assembleLotteryStrategy(100003L);*/
        strategyArmory.assembleLotteryStrategy(100006L);
        ReflectionTestUtils.setField(ruleWeightLogicChain, "userScore", 4500L);
        ReflectionTestUtils.setField(ruleLockLogicFilter, "userRaffleCount", 10);
    }


    @Test
    public void test_performRaffle(){
        RaffleFactorEntity jingouzhui = RaffleFactorEntity.builder()
                .userId("jingouzhui")
                .strategyId(100006L)
                .build();
        RaffleAwardEntity result = raffleStrategy.performRaffle(jingouzhui);
        log.info("请求参数：{}", JSON.toJSONString(jingouzhui));
        log.info("测试结果：{}",JSON.toJSONString(result));

    }

    @Test
    public void test_performRaffleCenter(){
        RaffleFactorEntity jingouzhui = RaffleFactorEntity.builder()
                .userId("jingouzhui")
                .strategyId(100003L)
                .build();
        RaffleAwardEntity result = raffleStrategy.performRaffle(jingouzhui);
        log.info("请求参数：{}", JSON.toJSONString(jingouzhui));
        log.info("测试结果：{}",JSON.toJSONString(result));

    }

    @Test
    public void test_blackList() throws InterruptedException {
        RaffleFactorEntity user001 = RaffleFactorEntity.builder()
                .userId("user001")
                .strategyId(100006L)
                .build();
        int i = 5;
        while (i >= 0) {
            RaffleAwardEntity result = raffleStrategy.performRaffle(user001);

            log.info("请求参数：{}", JSON.toJSONString(user001));
            log.info("测试结果：{}",JSON.toJSONString(result));
            i--;
        }
        // 等待 UpdateAwardStockJob 消费队列
        new CountDownLatch(1).await();

    }
}
